using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;


namespace WinCANTool
{
        
    public partial class EnterMessageWindow : Form
    {
        public NumericUpDown DLCInput = new NumericUpDown();
        public TextBox[] MyText = new TextBox[8];
        public TextBox IdentBox;
        public TextBox SendRateBox;
        public NewMessage CurrentMessage;

        public event NewMessageHandler NewData;
        
        protected virtual void MessageReady(NewEventArgs e)
        {
            
            if (NewData != null)
                NewData(this, e);
        }

       
        public EnterMessageWindow()
        {
            InitializeComponent();
            
            CurrentMessage = new NewMessage();
            CurrentMessage.SaveID_DLC_Int(0, 8);
            CurrentMessage.SaveSendRate(10);
            GetNewMessage(CurrentMessage);

            
        }


        public EnterMessageWindow(NewMessage Message)
        {
            InitializeComponent();
            CurrentMessage = new NewMessage(Message);
            GetNewMessage(CurrentMessage);
        }
       
        
        public void GetNewMessage(NewMessage Msg)
        {

           
            Label IdentLabel = new Label();
            IdentLabel.Text = "Identifier";
            IdentLabel.Location = new Point(10, 25);
            IdentLabel.Size = new Size(75, 20);


            Label SendRateLabel = new Label();
            SendRateLabel.Text = "Send Rate";
            SendRateLabel.Location = new Point(10, 90);
            SendRateLabel.Size = new Size(75, 20);


            SendRateBox = new TextBox();
            SendRateBox.Location = new Point(85, 90);
            SendRateBox.Size = new Size(30, 25);
            int ival = Msg.GetSendRate();
            string NewString = ival.ToString();
            SendRateBox.Text = NewString;



            IdentBox = new TextBox();
            IdentBox.Location = new Point(10, 50);
            IdentBox.Size = new Size(50, 25);
            IdentBox.MaxLength = 4;
            //int l = 0;
            uint l = Msg.GetIdentifier();
            string NewIdentString = l.ToString("X");
            IdentBox.Text = NewIdentString.PadLeft(4, '0');
            

            Label DLCLabel = new Label();
            DLCLabel.Text = "DLC";
            DLCLabel.Location = new Point(85, 25);
            DLCLabel.Size = new Size(40, 20);


            DLCInput.Location = new Point(85, 50);
            DLCInput.Size = new Size(30, 50);
            //DLCInput.Value = 4;
            DLCInput.Value = Msg.GetDLC();
            DLCInput.Maximum = 8;
            DLCInput.ValueChanged += new System.EventHandler(this.NewValue);


            Controls.Add(IdentLabel);
            Controls.Add(IdentBox);
            Controls.Add(SendRateLabel);
            Controls.Add(SendRateBox);
            Controls.Add(DLCLabel);
            Controls.Add(DLCInput);
            SetupDataBoxes(Msg.GetDLC());
           

        }


        private void NewValue(object sender, EventArgs e)
        {
            byte DLC = (byte) DLCInput.Value;
            SetupDataBoxes(DLC);
           
            
            
        }


        public void SetupDataBoxes(int DLCchoosen)
        {
            
            for (int i = 0; i < 8; i++)
            {
                if (MyText[i] != null)
                {
                    CurrentMessage.SaveData(i, MyText[i].Text);
                    this.Controls.Remove(MyText[i]);
                    MyText[i] = null;
                }
            }

            for (int i = 0; i < DLCchoosen; i++)
            {
                MyText[i] = new TextBox();
                MyText[i].Location = new Point(130 + 40 * i, 50);
                MyText[i].Size = new Size(30, 20);
                MyText[i].MaxLength = 2;

                int l = CurrentMessage.GetDataValue(i);
                string NewString = l.ToString("X");
                MyText[i].Text = NewString.PadLeft(2, '0');
                this.Controls.Add(MyText[i]);

            }
        }



        private void CancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void SaveData_Click(object sender, EventArgs e)
        {
            byte DLCtemp = (byte) DLCInput.Value;
            CurrentMessage.SaveID_DLC(IdentBox.Text, DLCtemp);
            int rate = Int32.Parse(SendRateBox.Text, NumberStyles.Integer);
            CurrentMessage.SaveSendRate(rate);

            for (int i = 0; i < DLCtemp; i++)
            {
                CurrentMessage.SaveData(i, MyText[i].Text);
            }

            NewEventArgs NewArgsMessage = new NewEventArgs();
            NewArgsMessage.EventMessage = CurrentMessage;
            MessageReady(NewArgsMessage);
            
            this.Dispose();
        }


    }
}